<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="状态" prop="sendStatus">
        <el-select
v-model="queryParams.sendStatus" 
        placeholder="状态" 
        class="!w-120px">
          <el-option
            v-for="dict in statusList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接收人" prop="memberUserName">
        <el-input
          v-model="queryParams.memberUserName"
          placeholder="请输入接收人"
          clearable
          @keyup.enter.stop="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="msgTelphone">
        <el-input
          v-model="queryParams.msgTelphone"
          placeholder="请输入接收人手机号码"
          clearable
          @keyup.enter.stop="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button type="primary" plain @click="handleAdd" v-hasPermi="['zwsj:huaxing-sms:create']">
          <Icon icon="ep:plus" /> 新增</el-button
        >
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" width="60" />
      <el-table-column label="接收人" align="center" prop="memberUserName" width="140">
        <template #default="scope">
          <label>{{ scope.row.memberUserName }}</label>
        </template>
      </el-table-column>
      <el-table-column label="发送的内容" align="left" prop="msgContent" />
      <el-table-column label="手机号码" align="center" prop="msgTelphone" width="110" />
      <el-table-column label="发送时间" align="center" prop="sendTime" width="180">
        <template #default="scope">
          <span>{{ formatDate(scope.row.sendTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已发送次数" align="center" prop="sendCount" width="100" />
      <el-table-column label="发送状态" align="center" prop="sendStatus" width="100">
        <template #default="scope">
          <label v-for="(s, i) in statusList" :key="i">
            <span v-if="s.value == scope.row.sendStatus">
              {{ s.label }}
            </span>
          </label>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="160"
      >
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="sendAgain(scope.row)"
            v-hasPermi="['zwsj:huaxing-sms:update']"
            >重新发送</el-button
          >
          <el-button
            type="danger"
            link
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zwsj:huaxing-sms:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <HuaxingSmsForm ref="formRef" @success="getList" />
</template>
<script lang="ts" setup>
import { formatDate } from '@/utils/formatTime'
import * as HuaxingSmsApi from '@/api/zwsj/huaxingSms'
import { ElNotification } from 'element-plus'
import HuaxingSmsForm from './HuaxingSmsForm.vue'

defineOptions({ name: 'HuaxingSms' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref() // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sendStatus: null,
  memberUserName: null,
  msgContent: null,
  msgTelphone: null
})

const statusList = reactive([
  {
    value: 0,
    label: '待发送'
  },
  {
    value: 1,
    label: '发送成功'
  },
  {
    value: -1,
    label: '发送失败'
  }
])

const queryFormRef = ref() // 搜索的表单

/** 查询部门列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await HuaxingSmsApi.getHuaxingSmsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.pageNo = 1
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await HuaxingSmsApi.deleteHuaxingSms(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 重新发送 **/
const sendAgain = (row) => {
  ElMessageBox.confirm('是否重新发送该内容?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      HuaxingSmsApi.sendHuaxingSmsAgain(row.id).then((data) => {
        if (data) {
          ElNotification({
            type: 'success',
            title: `成功`,
            message: '发送成功'
          })
        }
      })
    })
    .catch((e) => {
      // ElNotification({ type: 'info', message: e })
    })
}
const formRef = ref()
const handleAdd = () => {
  formRef.value.open('create')
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
